// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace GroceryList
{
    [Serializable]
    [DataContract]
    public class GroceryItem
    {
        private String _name;
        private int _quantity;

        public GroceryItem(String name, int quantity)
        {
            Contract.Requires(name != null);
            Contract.Requires(quantity > 0);

            _name = name;
            _quantity = quantity;
        }

        [DataMember]
        public String Name {
            get { return _name; }
            set { _name = value; }
        }

        [DataMember]
        public int Quantity {
            get { return _quantity; }
            set { _quantity = value; }
        }

        public override bool Equals(object obj)
        {
            GroceryItem that = obj as GroceryItem;
            if (that == null)
                return false;
            return Quantity == that.Quantity && Name == that.Name;
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }

        public override string ToString()
        {
            String result = Name;
            if (Quantity > 1)
                result += " x " + Quantity;
            return result;
        }
    }
}
